package com.aifast.core.db.handler;

import cn.hutool.core.util.IdUtil;
import com.aifast.system.sys.entity.UserInfo;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author fujian
 * @since 2021/12/30 16:31
 */
@Component
public class AiMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", () -> UserInfo.get().getRealName(), String.class);
        this.strictInsertFill(metaObject, "createUserId", () -> UserInfo.get().getUserId().longValue(), Long.class);
        this.strictInsertFill(metaObject, "userId", () -> UserInfo.get().getUserId(), Integer.class);
        this.strictInsertFill(metaObject, "orgId", () -> UserInfo.get().getOrgId(), Integer.class);
        this.strictInsertFill(metaObject, "snowflake", IdUtil::getSnowflakeNextId, Long.class);
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), String.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", () -> UserInfo.get().getRealName(), String.class);
        this.strictInsertFill(metaObject, "updateUserId", () -> UserInfo.get().getUserId().longValue(), Long.class);
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), String.class);
    }
}
